# set: 集合类似java的HashSet，不允许添加重复的元素
# 创建集合使用{}，列表使用[]
my_set1: set[int] = {1, 2, 3, 4, 1, 2}
print("my_set1", my_set1, "my_set数据类型: ", type(my_set1))

# list -> set
my_set2 = set([1, 2, 3, 1])  # list-->set
print("list -> set, 会进行去重", my_set2)

# tuple -> set
my_set3 = set((1, 2, 3, 2))  # tuple-->set
print("tuple转换成set, 会进行去重", my_set3)

# str -> set
my_set4 = set('12345123')  # str-->set
print("str -> set, 会进行去重", my_set4)

# dict -> set
my_set5 = set({"a": 1, "b": 2, "c": 3})  # dict-->set
print("dict转换成set, 只会遍历key", my_set5)

my_set6 = {1, 2, 3, 4}
# 成员函数
# in
print("成员函数in", 1 in my_set6)

# not in
print("成员函数not in", 5 not in my_set6)

# len
print("len函数", len(my_set6))

# min
print("min函数", min({1, 2, 3, 4}))

# 集合的遍历
for i in my_set6:
    print("每一个元素: ", i)

# 常用的方法
# add 添加元素
my_set6.add(9)
print("add函数", my_set6)

# remove 删除元素
my_set6.remove(1)
print("remove函数", my_set6)

# update 类似Set的addAll方法
my_set6.update({2, 3, 4, 5, 6, 7, 6, 5})
print("update方法", my_set6)

# 交集 并集
s2 = {5, 6, 7, 8, 9}
print(s2)
print(my_set6 & s2)
print(my_set6 | s2)

# 列表去重
score = [80, 70, 60, 80, 70, 60, 40]
my_set7 = set(score)
print(my_set7)
d = {}
# 统计各个分数都有几个学生
for i in my_set7:
    t = score.count(i)
    d[i] = t
for k, v in d.items():
    print(f"得分为{k}的学生有{v}个人")
